package com.doge.walknovel.business.strategy

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.doge.wnpro.R
import com.doge.walknovel.api.model.StrategyChildInfo
import com.doge.walknovel.api.model.StrategyInfo
import com.doge.walknovel.business.home.genres.TabHelper
import com.doge.walknovel.core.BaseActivity
import com.doge.walknovel.core.BaseFragment
import com.doge.wnpro.databinding.ActivityStrategyNovelBinding
import com.doge.walknovel.utils.fromJson
import com.doge.walknovel.utils.toJson
import com.google.gson.reflect.TypeToken

class StrategyNovelActivity : BaseActivity() {
    private val binding by lazy { ActivityStrategyNovelBinding.inflate(layoutInflater) }

    private val titleName by lazy { intent.getStringExtra(EXTRA_TITLE_NAME) }
    private val titleId by lazy { intent.getIntExtra(EXTRA_TITLE_ID, 0) }
    private val childTabs by lazy {
        var field = fromJson<List<StrategyChildInfo>>(
            intent.getStringExtra(EXTRA_CHILD_TABS),
            object : TypeToken<List<StrategyChildInfo>>() {}.type
        ) ?: listOf()
        if (field.isEmpty()) field = listOf(StrategyChildInfo.NONE)
        field
    }

    private val viewModel by viewModels<StrategyNovelViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.toolbar.findViewById<TextView>(R.id.pageTitle).text = titleName
        binding.viewPager.registerOnPageChangeCallback(pageChangeListener)
        setupViewPager()
    }

    private fun setupViewPager() {
        binding.viewPager.offscreenPageLimit = 2
        binding.viewPager.adapter = object : com.doge.walknovel.utils.FragmentStateAdapter(this) {
            override fun getTitle(position: Int): String {
                return childTabs[position].tabName ?: ""
            }

            override fun getItemCount(): Int = childTabs.size

            override fun createFragment(position: Int): Fragment {
                if (position < 0 || position >= childTabs.size) return BaseFragment()
                return StrategyNovelListFragment.newInstance(titleId, childTabs[position].tabId)
            }
        }
        binding.tab.isVisible = !childTabs.contains(StrategyChildInfo.NONE)
        TabHelper.setupCustomTab(binding.tab, binding.viewPager, childTabs)
        viewModel.updateCurrentTab(0)
    }

    private val pageChangeListener = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            viewModel.updateCurrentTab(position)
        }
    }

    companion object {
        private const val EXTRA_TITLE_NAME = "titleName"
        private const val EXTRA_TITLE_ID = "titleId"
        private const val EXTRA_CHILD_TABS = "childTabs"

        fun start(context: Context?, data: StrategyInfo?) {
            if (context == null || data == null) return
            context.startActivity(Intent(context, StrategyNovelActivity::class.java).apply {
                putExtra(EXTRA_TITLE_NAME, data.titleName)
                putExtra(EXTRA_TITLE_ID, data.titleId)
                putExtra(EXTRA_CHILD_TABS, toJson(data.tabs ?: listOf<StrategyChildInfo>()))
            })
        }
    }

}